package com.laicunba.event.listener;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.laicunba.event.PaySuccessEvent;
import com.laicunba.event.RateIncrCouponCreateEvent;
import com.laicunba.pojo.FinancingTargetBidDO;
import com.laicunba.pojo.RateIncrCouponDO;
import com.laicunba.pojo.RateIncrCouponDefDO;
import com.laicunba.pojo.RegistryDO;
import com.laicunba.service.RateIncrCouponDefService;
import com.laicunba.service.RegistryService;

@Component
public class TeachersDayListener implements ApplicationListener<PaySuccessEvent>, ApplicationEventPublisherAware {
	private static final Logger LOG = Logger.getLogger(TeachersDayListener.class);

	private ApplicationEventPublisher applicationEventPublisher;
	@Autowired
	private RegistryService registryService;
	@Autowired
	private RateIncrCouponDefService rateIncrCouponDefService;

	@Override
	public void onApplicationEvent(PaySuccessEvent event) {
		try {
			FinancingTargetBidDO bidDO = (FinancingTargetBidDO) event.getSource();
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(bidDO != null && bidDO.getType().equalsIgnoreCase("CPB") && new Date().compareTo(sdf.parse("2016-09-09 00:00:00")) >= 1 && new Date().compareTo(sdf.parse("2016-09-13 00:00:00")) < 1) {
				RegistryDO rewardCountRegistry = registryService.get(RegistryDO.class, "u." + bidDO.getBidUserId() + ".teachers-day.reward.count");
				LOG.info(String.format("【教师节活动】用户投资送加息券，订单id为[%s],userId=[%s],标的id为[%s],标的类型为[%s],购买金额为[%s],payMark为[%s],下单时间为[%s],", bidDO.getId(), bidDO.getBidUserId(), bidDO.getFinancingTargetId(), bidDO.getType(), bidDO.getBuyAmount(), bidDO.getPayMark(), bidDO.getPayTime()));
				if(rewardCountRegistry == null) {
					RateIncrCouponDefDO rateIncrDef = rateIncrCouponDefService.get(RateIncrCouponDefDO.class, "act-teachers-day-rate-incr-1");
					if (rateIncrDef == null) {
						LOG.error(String.format("【教师节活动】发放加息券出错，加息券未定义，rateIncrDef为[%s]", rateIncrDef));
					} else if(rateIncrDef.getUsedQuantity() >= rateIncrDef.getTotalQuantity() || (rateIncrDef.getEndTime() != null && new Date().after(rateIncrDef.getEndTime()))) {
						LOG.error(String.format("【教师节活动】发放加息券出错，加息券已发完或加息券信息已过期，已发放[%s]张，发行量为[%s]，加息券定义结束时间为[$s]，当前时间为[%s]", rateIncrDef.getUsedQuantity(), rateIncrDef.getTotalQuantity(), rateIncrDef.getEndTime(), new Date()));
					} else {
						RateIncrCouponDO rateIncrCouponDO = rateIncrCouponDefService.send2User(rateIncrDef.getId(), bidDO.getBidUserId(), true, "用户在活动期间投资定期理财送加息券");
						RateIncrCouponCreateEvent rateIncrEvent = new RateIncrCouponCreateEvent(rateIncrCouponDO);
						applicationEventPublisher.publishEvent(rateIncrEvent);
						//标记用户已发放加息券
						rewardCountRegistry = new RegistryDO();
						rewardCountRegistry.setId("u." + bidDO.getBidUserId() + ".teachers-day.reward.count");
						rewardCountRegistry.setVal("1");
						rewardCountRegistry.setVisible(true);
						registryService.create(rewardCountRegistry);
					}
				}
			}
		} catch (Exception e) {
			LOG.error("【教师节活动】加息券发放出错了", e);
		}
	}

	@Override
	public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
		this.applicationEventPublisher = applicationEventPublisher;
	}
	
}
